package com.feature.core.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;

/**
 * 当消息在消费者端处理失败时，MessageRecoverer 可以定义相应的恢复策略，例如重试消息、将消息发送到死信队列或者记录日志等。
* @Description:
* @author wangzhiyue
* @date 2025/04/29 10:08:34
 */
public class LogMessageRecoverer implements MessageRecoverer {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void recover(Message message, Throwable cause) {
		MessageProperties messageProperties = message.getMessageProperties();
		String traceID = String.valueOf(messageProperties.getHeaders().get("traceID"));
		String error = this.getStackTraceAsString(cause);
		this.logger.info("消息接收失败N次状态提示,TRACEID:{},MSGID:{},消息体:[{}],异常信息:[{}],详情请查看数据收集平台", new Object[] { traceID, messageProperties.getMessageId(), message.toString(), error });
		logger.error("",cause);
	}

	private String getStackTraceAsString(Throwable t) {
		StringBuilder builder = new StringBuilder();
		builder.append("MESSAGE:").append(t.getMessage()).append(" CAUSE:").append(t.toString());
		return builder.toString();
	}
}
